﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class popup : System.Web.UI.Page
{
    public SqlConnection conn;
    public SqlCommand cmd;
    public String Text;
    public String Datatable;                //chua key cua table tuong ung trong sql
    String textboxid;                       // id cua text box gay ra popup
    String textboxnext;                     //xac dinh textbox duoc focus sau khi popup
    String key;                             // key xac dinh su kien postback duoc goi hay khong
    public SqlDataReader dr;
    public DataSet ds;
    public SqlDataAdapter da;
    DataTable dt;
    String flag;                            //xac dinh cho phep an hien checkbox trong gridview
    String Can_le;                          //chuoi xac dinh can len cho gridview
    protected void Page_Load(object sender, EventArgs e)
    {
        Text = Request.QueryString["Text"];
        Datatable = Request.QueryString["Datatable"];
        textboxid = Request.QueryString["textboxid"];
        textboxnext = Request.QueryString["key"];
        key = Request.QueryString["textboxnext"];
        lbh.Attributes.Add("onclick", "chon(\"" + textboxid + "\")");  
     

        datasever data = new datasever();
        conn = data.getconnect();
        conn.Open();

        //lay id hidden popup cho phep hien checkbox hay khong
        cmd = new SqlCommand("sp_select_popuphidden", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Ma", Datatable));
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            flag = dr["Hien"].ToString();
            if (flag == "1")
            {
                gv.Columns[0].Visible = false;
                lbh.Visible = false;
            }
            dr.Close();
        }

        //lay co cot can can le
        cmd = new SqlCommand("sp_select_can_le", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Ma", Datatable));
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds,"canle");
        Can_le = ds.Tables["canle"].Rows[0][0].ToString();

        //day du lieu vao girdview
        gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
        cmd = new SqlCommand("sp_select_du_lieu", conn);        
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Ma", Datatable));
        cmd.Parameters.Add(new SqlParameter("@Text", Text));
        da = new SqlDataAdapter(cmd);
        ds= new DataSet();
        da.Fill(ds, "table");

        //neu dieu kien tim kiem khong co trong table thi lay tat ca table
        if (ds.Tables["table"].Rows.Count == 0)
        {
            cmd = new SqlCommand("sp_select_du_lieu", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Ma", Datatable));
            cmd.Parameters.Add(new SqlParameter("@Text", ""));
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "table");
        }
        gv.DataSource = ds.Tables["table"];
        gv.DataBind();
        conn.Close();
        
    }

    void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //can len cho cot la so
        MatchCollection mc;
        string chuoi = Can_le;
        chuoi = chuoi + " ";
        //tạo pattern
        //luật:cho tìm ra bất cứ những ký tự không phải ký tự khoảng trắng
        //rồi theo sau nó là kí tự khoảng trắng
        string pattern = @"\S+\s";
        Regex myRegex = new Regex(pattern);
        mc = myRegex.Matches(chuoi);
        for (int j = 0; j < mc.Count; j++)
        {
            if (mc[j].Value == "T ")
            {
                e.Row.Cells[j].HorizontalAlign = HorizontalAlign.Right;
            }
        }

       //tao hyperlinkfield cho cot dau tien
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            if (key == "1")
            {
                key = ",1";
            }
            else
            {
                key = "";
            }
            string HyperLinkValue = e.Row.Cells[1].Text;
            HyperLink myLink = new HyperLink();
            myLink.NavigateUrl = "#";
            myLink.Text = HyperLinkValue;
            myLink.Attributes.Add("onclick", "load(\"" + HyperLinkValue + "\"," + "\"" + textboxid + "\",\"" + textboxnext + "\"" + ")");            
            e.Row.Cells[1].Controls.Add(myLink);
        }
        
    }

}